<?php
namespace app\common\controller;

/**
 * 空间信息类
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/25
 * Time: 10:00
 */
class Space{

    /**
     * 获取概述信息
     * @param $wsids
     * @return array
     */
    public function get_overview($wsids){
        //查找已巡店工位（最新巡店工位）
        $crwids = [];
        foreach($wsids as $k => $v){
            $maps = [];
            $maps[] = ['wsid','=',$v];
            $crw_detail = model('cp_region_ws')->get_detail($maps,'id','id desc');
            $crwids[] = !empty($crw_detail) ? $crw_detail['id'] : 0;
        }

        $now_date = date('Y-m-d');

        //计算总数量
        $total_num = count($wsids);

        //计算空置工位数量
        $maps = [];
        $maps[] = ['id','in',$wsids];
        $maps[] = ['company_out_time','<',$now_date];
        $vacant_ws_num = model('work_station')->get_count($maps,'id');

        //计算办公人数
        $maps = [];
        $maps[] = ['id','in',$crwids];
        $maps[] = ['use_status','=',1];
        $maps[] = ['status','=',1];
        $person_num = model('cp_region_ws')->get_sum($maps,'person_num');

        //计算已使用工位数量
        $maps = [];
        $maps[] = ['id','in',$wsids];
        $maps[] = ['company_out_time','>=',$now_date];
        $use_ws_num = model('work_station')->get_count($maps,'id');

        //计算总体出租率
        $use_rate = (count($wsids) > 0) ? $use_ws_num / count($wsids) : 0;
        $use_rate = $use_rate * 100;
        $use_rate = round($use_rate,0);

        $data = [];
        $data['total_num'] = $total_num;
        $data['vacant_ws_num'] = $vacant_ws_num;
        $data['person_num'] = $person_num;
        $data['use_rate'] = $use_rate;
        $data['use_ws_num'] = $use_ws_num;
        return $data;
    }

    /**
     * 获取空间工位信息
     * @param $regionid
     * @param $wsid
     * @param $company_out_time
     * @return array
     */
    public function get_space_ws_info($regionid,$wsid,$company_out_time){
        $data = [];
        $now_date = date('Y-m-d');

        if($company_out_time < $now_date){
            $data['use_status'] = 0;
            $data['use_status_zw'] = '空置';
            $data['use_rate'] = 0;
            $data['person_num'] = 1;
            $data['is_holiday_work'] = 0;
            $data['is_evening_work'] = 0;
            $data['ccids'] = [];
            $data['remark'] = '';
            $data['status'] = 1;
            $data['status_zw'] = '已完成';
        }else{
            $data['use_status'] = 2;
            $data['use_status_zw'] = '无人';
            $data['use_rate'] = 0;
            $data['person_num'] = 1;
            $data['is_holiday_work'] = 0;
            $data['is_evening_work'] = 0;
            $data['ccids'] = [];
            $data['remark'] = '';
            $data['status'] = 1;
            $data['status_zw'] = '已完成';

            $maps[] = ['regionid','=',$regionid];
            $maps[] = ['wsid','=',$wsid];
            $maps[] = ['status','=',1];
            $crs_detail = model('cp_region_ws')->get_detail($maps,'id,use_status,use_rate,person_num,is_holiday_work,is_evening_work,ccids,remark,status','id desc');
            if(!empty($crs_detail)){
                $data['use_status'] = $crs_detail['use_status'];
                $data['use_status_zw'] = $crs_detail->use_status_name;
                $data['use_rate'] = $crs_detail['use_rate'];
                $data['person_num'] = $crs_detail['person_num'];
                $data['is_holiday_work'] = $crs_detail['is_holiday_work'];
                $data['is_evening_work'] = $crs_detail['is_evening_work'];
                $data['ccids'] = json_decode($crs_detail['ccids'],true);
                $data['remark'] = $crs_detail['remark'];
                $data['status'] = $crs_detail['status'];
                $data['status_zw'] = $crs_detail->status_name;
            }
        }
        return $data;
    }

    /**
     * 处理空间工位“是否加班”显示
     * @return mixed
     */
    public function deal_space_is_show_work(){
        $is_show_evening_work = 1;
        $is_show_holiday_work = 1;

        $data['is_show_evening_work'] = $is_show_evening_work;
        $data['is_show_holiday_work'] = $is_show_holiday_work;
        return $data;
    }

    /**
     * 计算空间工位使用率
     * @param $cp_type
     * @param $wsid
     * @return mixed
     */
    public function count_space_ws_use_rate($cp_type,$wsid){
        $maps = [];
        $maps[] = ['cp_type','=',$cp_type];
        $maps[] = ['wsid','=',$wsid];
        $total_num = model('cp_region_ws')->get_count($maps,'id');

        $maps = [];
        $maps[] = ['cp_type','=',$cp_type];
        $maps[] = ['wsid','=',$wsid];
        $maps[] = ['use_status','=',1];
        $use_num = model('cp_region_ws')->get_count($maps,'id');

        $use_rate = ($total_num > 0) ? $use_num / $total_num : 0;
        $use_rate = $use_rate * 100;
        $data['use_rate'] = round($use_rate,0);
        return $data;
    }
}